<?php
/**
 * Class name  is ConfigSysinfoController * @package backend\modules\common\controllers;
 * @author  Womtech  email:chareler@163.com
 * @DateTime 2020-03-05 16:44 
 */
namespace backend\modules\common\controllers;

use Yii;
use common\models\config\ConfigSysinfo;
use common\models\config\ConfigSysinfoSearch;
use backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\utils\ToolUtil;

/**
 * ConfigSysinfoController implements the CRUD actions for ConfigSysinfo model.
 */
class ConfigSysinfoController extends BaseController
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all ConfigSysinfo models.
     * @return mixed
     */
    public function actionIndex()
    {

        return $this->render('index', [

        ]);
    }

    /**
     * 获取Sysinfo的所有数据
     *
     * @return void
     */
    public function actionGetSysinfoList(){       
        
        $searchModel = new ConfigSysinfoSearch();
        $searchModel->scenario = 'search';
        $dataProvider = $searchModel->search(Yii::$app->request->getBodyParams());
        if($searchModel->hasErrors()){
            $return=ToolUtil::returnAjaxMsg(false,'参数有误', ['errors'=>$searchModel->errors]);
            return $this->asJson($return);
        }

        
        $count = $dataProvider->getTotalCount();        
        return $this->asJson([
            'code' => 0,
            'msg' => '',
            'count' => (int)$count,
            'data' => $dataProvider->getModels(),
        ]);

        


        /* $query = ConfigBase::find()->filterWhere([]);
        return $this->getJqTableData($query); */

       /*  $authName = $this->post('authName');
        $start = $this->post('start');
        $end = $this->post('end');
        $query = AuthItem::find()->filterWhere(['AND',['type' => 1],['like','description',$authName]]);
        if(!empty($start)){
            $startTime = strtotime($start. " 00:00:00");
            $query->andWhere(['>=','created_at',$startTime]);
        }
        if(!empty($end)){
            $endTime = strtotime($end. " 23:59:59");
            $query->andWhere(['<=','created_at',$endTime]);
        }
        $this->sidx = 'order_sort DESC,created_at';
        return $this->getJqTableData($query); */


    }





    /**
     * Displays a single ConfigSysinfo model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new ConfigSysinfo model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $request = Yii::$app->request;      

        //如果是表单get提交,如果有参数id,则是更改,没有则是新增
        if($request->isGet){
            $id = $this->get('siteid');
            if(empty($id)){
                $model = new ConfigSysinfo();  
                return $this->render('create', [
                ]);
            }else{
                $model = $this->findModel($id);
                return $this->render('create', [
                    'model' => $model,
                ]);

            }
        }

        if($request->isPost){
            //$id = yii::$app->request->post()["ConfigBase"]["id"];
            $id = yii::$app->request->post("ConfigSysinfo")["siteid"];

            //如果没有id,说明是新增
            if(empty($id)){
                $model = new ConfigSysinfo();  
                if ($model->load($request->post()) && $model->save()) {                   
                    return ToolUtil::returnAjaxMsg(true,'添加成功');
                } 

            }else{
                $model = $this->findModel($id);
                if ($model->load($request->post()) && $model->save()){
                    $return=ToolUtil::returnAjaxMsg(true,'修改成功');
                }else{
                    $return=ToolUtil::returnAjaxMsg(false,'修改失败');
                }
                return $this->asJson($return);
            }

        }



        /* $model = new ConfigSysinfo();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->siteid]);
        }

        return $this->render('create', [
            'model' => $model,
        ]); */



    }

    /**
     * Updates an existing ConfigSysinfo model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->siteid]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing ConfigSysinfo model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete()
    {
        $request = Yii::$app->request;
        $id = $request->post("siteid");  //数组格式
        $type=$request->post("type","hard");     
        $result = false;
        //$arrIds = explode(",",$id);
        foreach($id as $item){
            $model = $this->findModel($item);
            switch($type){
                case 'hard':
                    $result =  $model->delete();
                    break;
                case 'soft':
                    $model->status = -1;
                    $result = $model->save();
                    break;
                default:
                    break;
            }

            if(!($result)){
                return $this->asJson(ToolUtil::returnAjaxMsg(false,'删除失败'));
            }

        }
        

        $return=($result) ? ToolUtil::returnAjaxMsg(true,'删除成功') : ToolUtil::returnAjaxMsg(false,'删除失败');
        return $this->asJson($return);


       /*  $this->findModel($id)->delete();

        return $this->redirect(['index']); */
    }

    /**
     * Finds the ConfigSysinfo model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ConfigSysinfo the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ConfigSysinfo::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
